import React from "react"
import errorCodes from "./errorCodes"

import {useHistory} from "react-router-dom"
import service from "./index"

export default function useService(asyncFn, effect =[]) {
	var history = useHistory()
	React.useEffect(() => {
		(async () => {
			try {
				await asyncFn(service)
			}catch(e) {
				if(e.State &&e.State == errorCodes.UserNoSignin){
					history.push("/")
				}
				throw e
			}
		})();
	}, effect);
}